package com.niiwoo.civet.mobile.service.bill;

import com.alibaba.dubbo.config.annotation.Reference;
import com.niiwoo.civet.account.dto.response.UserBillBaseResponseDTO;
import com.niiwoo.civet.account.enums.OrderTypeEnum;
import com.niiwoo.civet.base.enums.BorrowProjectTypeEnum;
import com.niiwoo.civet.mobile.vo.response.bill.JumpTypeEnum;
import com.niiwoo.civet.mobile.vo.response.bill.UserBillDetailResponse;
import com.niiwoo.civet.trade.dto.response.ProjectDetailDTO;
import com.niiwoo.civet.trade.dto.response.creditor.CreditorTransferRemainingDayAndMonthDTO;
import com.niiwoo.civet.trade.dto.response.intelligenceInvest.ProjectPackageResponseDTO;
import com.niiwoo.civet.trade.service.creditor.CreditorSaleOrderDubboService;
import com.niiwoo.civet.trade.service.intelligenceInvest.IntelligenceInvestDubboService;
import com.niiwoo.civet.trade.service.invest.InvestOrderDubboService;
import com.niiwoo.civet.trade.service.project.ProjectDubboService;
import com.niiwoo.tripod.base.plan.support.PeriodUnitEnum;
import com.niiwoo.tripod.base.utils.BigDecimals;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * Created by Yijian on 2018/11/22.
 */
@Slf4j
@Service
public class UserBillInvestOrderService {

    @Reference(version = "1.0.0")
    private ProjectDubboService projectDubboService;
    @Reference(version = "1.0.0")
    private IntelligenceInvestDubboService intelligenceInvestDubboService;
    @Reference(version = "1.0.0")
    private InvestOrderDubboService investOrderDubboService;
    @Reference(version = "1.0.0")
    private CreditorSaleOrderDubboService creditorSaleOrderDubboService;

    public String getInvestOrderContent(UserBillBaseResponseDTO userBillBase, UserBillDetailResponse response){
        StringBuilder sbd = new StringBuilder();

        boolean isZNT = false;//是否智能投
        if(OrderTypeEnum.ZNT_INVEST.getCode().equals(userBillBase.getOrderType())){
            //投资智能投包
            isZNT = true;
        }else if(Objects.nonNull(userBillBase.getProjectId()) && !userBillBase.getProjectId().equals(userBillBase.getProjectPackageId())){
            isZNT = true;
        }

        //组装投资订单
        if (isZNT) {
            //智能投
            ProjectPackageResponseDTO projectPackageResponseDTO = intelligenceInvestDubboService.getIntelligenceInvestInfo(userBillBase.getProjectPackageId());

            log.info("=================queryProjectPackageInfo: deadlineUnit:{},deadline:{},borrowRate:{},activityRaiseRate:{}",
                    projectPackageResponseDTO.getDeadlineUnit(),projectPackageResponseDTO.getDeadline(),
                    projectPackageResponseDTO.getBorrowRate(),projectPackageResponseDTO.getActivityRaiseRate());

            if (PeriodUnitEnum.MONTH.getValue().equals(projectPackageResponseDTO.getDeadlineUnit())) {
                sbd.append(projectPackageResponseDTO.getDeadline()).append("个月");
            } else if (PeriodUnitEnum.DAY.getValue().equals(projectPackageResponseDTO.getDeadlineUnit())) {
                sbd.append(projectPackageResponseDTO.getDeadline()).append("天");
            }
            sbd.append(" ").append(BigDecimals.formatBorrowRate(projectPackageResponseDTO.getBorrowRate())).append("%");
            if (projectPackageResponseDTO.getActivityRaiseRate() != null && projectPackageResponseDTO.getActivityRaiseRate().compareTo(BigDecimal.ZERO) == 1) {
                sbd.append("+").append(BigDecimals.formatBorrowRate(projectPackageResponseDTO.getActivityRaiseRate())).append("%");
            }
        } else {

            // 信用标和债转
            Long projectId = Long.valueOf(userBillBase.getProjectId());
            // 如果是债转先查询原标的id,并查询剩余天、月
            String remainingDayAndMonth = "";
            if (BorrowProjectTypeEnum.TRANSFER_OF_CREDIT.getValue().equals(userBillBase.getProjectType())) {
                CreditorTransferRemainingDayAndMonthDTO remainingDayAndMonthDTO = new CreditorTransferRemainingDayAndMonthDTO();
                // 账单类型为投资，根据投资订单ID查原始标的
                if(OrderTypeEnum.INVEST.getCode().equals(userBillBase.getOrderType())){
                    projectId = investOrderDubboService.getProjectIdByInvestOrderId(Long.valueOf(userBillBase.getOrderId()));
                    // 债转标展示剩余月、天
                    remainingDayAndMonthDTO = creditorSaleOrderDubboService.getTransferRemainingDayAndMonth(Long.valueOf(userBillBase.getOrderId()),null);
                } else if (OrderTypeEnum.RECEIVED_REPAYMENT.getCode().equals(userBillBase.getOrderType())){
                    remainingDayAndMonthDTO = creditorSaleOrderDubboService.getTransferRemainingDayAndMonth(null,Long.valueOf(userBillBase.getOrderId()));
                }

                if (remainingDayAndMonthDTO.getRemainingMonth() > 0) {
                    remainingDayAndMonth += remainingDayAndMonthDTO.getRemainingMonth() + "个月";
                }
                if (remainingDayAndMonthDTO.getRemainingDay() > 0) {
                    remainingDayAndMonth += remainingDayAndMonthDTO.getRemainingDay() + "天";
                }

            }
            ProjectDetailDTO projectDetailDTO = projectDubboService.getProjectById(projectId);
            if (projectDetailDTO != null) {
                log.info("=================projectDubboService.getProjectById: deadlineUnit:{},deadline:{},borrowRate:{},activityRaiseRate:{}",
                        projectDetailDTO.getDeadlineUnit(),projectDetailDTO.getDeadline(),projectDetailDTO.getBorrowRate(),projectDetailDTO.getActivityRaiseRate());

                // 债转标展示剩余月、天
                if (StringUtils.isNotBlank(remainingDayAndMonth)) {
                    sbd.append(remainingDayAndMonth);
                } else {
                    // 信用标展示标的期限
                    if (PeriodUnitEnum.MONTH.getValue().equals(projectDetailDTO.getDeadlineUnit())) {
                        sbd.append(projectDetailDTO.getDeadline()).append("个月");
                    } else if (PeriodUnitEnum.DAY.getValue().equals(projectDetailDTO.getDeadlineUnit())) {
                        sbd.append(projectDetailDTO.getDeadline()).append("天");
                    }
                }
                sbd.append(" ").append(BigDecimals.formatBorrowRate(projectDetailDTO.getBorrowRate())).append("%");
                if (projectDetailDTO.getActivityRaiseRate() != null && projectDetailDTO.getActivityRaiseRate().compareTo(BigDecimal.ZERO) == 1) {
                    sbd.append("+").append(BigDecimals.formatBorrowRate(projectDetailDTO.getActivityRaiseRate())).append("%");
                }
            }
        }

        return sbd.toString();
    }
}
